package com.tapstitch.scm.common.utils.excel;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

public abstract class ExportFile {

    protected String fileSuffix;
    protected String fileName;
    protected OutputStream outputStream;
    protected byte[] byteArray;
    protected String contentType;


    protected ExportFile(String fileName, byte[] byteArray, String fileSuffix, String contentType){
        this(fileName,fileSuffix,contentType);
        this.byteArray = byteArray;
    }

    protected ExportFile(String fileName, OutputStream outputStream, String fileSuffix, String contentType){
        this(fileName,fileSuffix,contentType);
        this.outputStream = outputStream;
    }

    private ExportFile(String fileName, String fileSuffix, String contentType){
        this.fileName = fileName;
        this.fileSuffix = fileSuffix;
        this.contentType = contentType;
    }

    public String getFileName() {
        return fileName+ fileSuffix;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public OutputStream getOutputStream() {
        return outputStream;
    }

    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public byte[] getByteArray() {
        return byteArray;
    }

    public void setByteArray(byte[] byteArray) {
        this.byteArray = byteArray;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    /**
     * 将流转成字节数组并关闭流
     * @return
     */
    public byte[] toByteArray(){
        if (outputStream == null){
            return this.byteArray;
        }
        try {
            ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) outputStream;
            this.byteArray = byteArrayOutputStream.toByteArray();
            //关闭流
            outputStream.close();
            outputStream = null;
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.byteArray;
    }

}
